package com.agileboot.common.core.page;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 基础分页查询对象
 * 提供通用的分页查询功能，兼容AbstractPageQuery和PageQuery的功能
 *
 * @author agileboot
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BasePageQuery extends PageQuery<Object> {

    /**
     * 排序列
     */
    protected String orderColumn;

    /**
     * 排序方向 ascending/descending
     */
    protected String orderDirection;

    /**
     * 构建通用的查询条件
     *
     * @return 查询条件包装器
     */
    public QueryWrapper<Object> buildQueryWrapper() {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        
        // 添加排序条件
        if (orderColumn != null && !orderColumn.isEmpty()) {
            boolean isAsc = "ascending".equals(orderDirection);
            queryWrapper.orderBy(true, isAsc, camelToUnderscore(orderColumn));
        }
        
        return queryWrapper;
    }
    
    /**
     * 驼峰命名转下划线命名
     *
     * @param camelCase 驼峰命名字符串
     * @return 下划线命名字符串
     */
    private String camelToUnderscore(String camelCase) {
        if (camelCase == null || camelCase.isEmpty()) {
            return "";
        }
        
        StringBuilder result = new StringBuilder();
        result.append(Character.toLowerCase(camelCase.charAt(0)));
        
        for (int i = 1; i < camelCase.length(); i++) {
            char ch = camelCase.charAt(i);
            if (Character.isUpperCase(ch)) {
                result.append('_');
                result.append(Character.toLowerCase(ch));
            } else {
                result.append(ch);
            }
        }
        
        return result.toString();
    }
} 